package com.cms.recursoshumanos.component.base;

import java.util.Vector;

import com.cms.recursoshumanos.object.ArticuloCarrito;
import com.cms.recursoshumanos.object.list.ListaArticuloCarrito;

import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.FontFamily;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.GridFieldManager;

public class GridField extends Field
{

	int WIDTH = 0;
	int HEIGHT = 0;
	int x = 0;
	int y = 0;
	int numRows = 0;
	int GRID_WIDTH = 30;
	int GRID_HEIGHT = 30;
	int fill_width=20;
	int fill_height=20;
	int currentColumn = 0;
	int currentRow = 0;
	public String[][] values;
	int[][] numbers = null;
	private Font numericFont = null;
	private Font alphabetFont = null;  
	Graphics G;
	int clu = 0;
	int length=0;
	char di;
	
	private ListaArticuloCarrito lista_articulo;
	
	public GridField(int width, ListaArticuloCarrito lista_articulo) 
	{
	    this.WIDTH = width;
	    this.lista_articulo = lista_articulo;
	    //HEIGHT = 3;
	    this.numRows = lista_articulo.cantElementListaArticulo();
	    pintargrid();
	}
	public int getPreferredWidth()  
	{  
	    return WIDTH;  
	}  
	
	public int getPreferredHeight()  
	{  
	    return HEIGHT;  
	}  
	
	protected void onFocus(int direction)  
	{  
	    System.out.println("onfocus direction =====>"+direction);
	}  
	
	protected void onUnfocus()  
	{  
	    System.out.println("onunfocus direction =====>");
	}  
	
	protected void layout(int arg0, int arg1)  
	{  
	    setExtent(getPreferredWidth(), getPreferredHeight());  
	}  
	
	public void paint(int clue, int len, char dir)
	{
	    try 
	    {
	        clu=clue;
	        length=len;
	        di=dir;
	        FontFamily fontFamily = null;
	        try 
	        {
	            fontFamily = FontFamily.forName("Verdana");
	        } 
	        catch (ClassNotFoundException cnfe) 
	        {
	        }   
	        numericFont = fontFamily.getFont(FontFamily.SCALABLE_FONT, 9);
	        alphabetFont = fontFamily.getFont(FontFamily.SCALABLE_FONT, 13);    
	        //G.clear();
	        G.setColor(0x00ffffff);
	        G.fillRect(0, 0, WIDTH, HEIGHT);
	        G.setColor(0x00000000);
	        y = 5;
	        for(int i = 0; i < numRows; i ++ )
	        {
	            for(int j = 0; j < numRows; j ++ )
	            {
	                G.setColor(Color.BLACK);
	                G.drawRect(x, y, GRID_WIDTH, GRID_HEIGHT);                 
	                G.setColor(Color.BLUE);
	                // G.setFont(alphabetFont);
	                //G.setFont(Utility.getInstance().getGujaratiFont("rekha", 4));
	                // if(values[i][j]!=".")
	                // G.drawText("" + values[i][j], x + 13, y + 11);
	                if(values[i][j]!=".")
	                    G.drawText("" + values[i][j], x + 5, y + 11);
	                G.setColor(Color.BLACK);
	                G.setFont(numericFont);
	                if (numbers[i][j] == clue)
	                {
	                    currentColumn=j;
	                    currentRow=i;
	                    G.setColor(Color.RED);
	                    G.fillRect(x, y, GRID_WIDTH, GRID_HEIGHT);
	
	                    G.setColor(Color.ORANGE); 
	                    if(dir=='a'||dir=='A')
	                    {
	
	                        G.drawRect(x+GRID_WIDTH, y, 30*(len-1), GRID_HEIGHT);   
	                        //                      G.drawRect(x-1, y-1, 30*len+2, GRID_HEIGHT+2); 
	                        //                      G.drawRect(x-2, y-2, 30*len+3, GRID_HEIGHT+3);  
	                        //                      G.drawRect(x-3, y-3, 30*len+4, GRID_HEIGHT+4); 
	                        G.fillRect(x+GRID_WIDTH, y, 30*(len-1), GRID_HEIGHT);
	                    }
	                    else
	                    {
	                        G.drawRect(x, y+GRID_HEIGHT, GRID_WIDTH, 30*(len-1));  
	                        G.fillRect(x, y+GRID_HEIGHT, GRID_WIDTH, 30*(len-1));
	                    }
	
	
	                }
	
	                G.setColor(Color.BLACK);
	                if(numbers[i][j]!=0)
	                    G.drawText("" + numbers[i][j],x + 2  ,y + 2);
	                if(values[i][j] == "0"){
	                    G.setColor(Color.BLACK);
	                    G.fillRect(x, y, GRID_WIDTH, GRID_HEIGHT);
	                    //Field.VISUAL_STATE_FOCUS;
	                }
	                x = x + GRID_WIDTH - 1;      
	            }
	            x = 0;
	            y = y + GRID_HEIGHT - 1;    
	        }
	        //this.invalidate();
	    } 
	    catch (Exception e) 
	    {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    }
	}
	
	protected void paint(Graphics g)
	{
	
	    FontFamily fontFamily = null;
	    try 
	    {
	        fontFamily = FontFamily.forName("Verdana");
	    }
	    catch (ClassNotFoundException cnfe) 
	    {
	    	
	    }   
	    try 
	    {
	        numericFont = fontFamily.getFont(FontFamily.SCALABLE_FONT, 9);
	        alphabetFont = fontFamily.getFont(FontFamily.SCALABLE_FONT, 13);
	        G=g;
	        g.setColor(0x00ffffff);
	        g.fillRect(0, 0, WIDTH, HEIGHT);
	        g.setColor(0x00000000);
	        y = 5;
	        for(int i = 0; i < numRows; i ++ )
	        {
	            for(int j = 0; j < numRows; j ++ )
	            {
	                g.setColor(Color.BLACK);
	                g.drawRect(x, y, GRID_WIDTH, GRID_HEIGHT);  
	                if (clu != 0)
	                {
	                    if (numbers[i][j] == clu)
	                    {
	                        currentColumn=j;
	                        currentRow=i;
	                        G.setColor(Color.RED);
	                        G.fillRect(x, y, GRID_WIDTH, GRID_HEIGHT);
	
	                        G.setColor(Color.ORANGE); 
	                        if(di=='a'||di=='A')
	                        {
	
	                            G.drawRect(x+GRID_WIDTH, y, 30*(length-1), GRID_HEIGHT);   
	                            //                          G.drawRect(x-1, y-1, 30*len+2, GRID_HEIGHT+2); 
	                            //                          G.drawRect(x-2, y-2, 30*len+3, GRID_HEIGHT+3);  
	                            //                          G.drawRect(x-3, y-3, 30*len+4, GRID_HEIGHT+4); 
	                            G.fillRect(x+GRID_WIDTH, y, 30*(length-1), GRID_HEIGHT);
	                        }
	                        else
	                        {
	                            G.drawRect(x, y+GRID_HEIGHT, GRID_WIDTH, 30*(length-1));  
	                            G.fillRect(x, y+GRID_HEIGHT, GRID_WIDTH, 30*(length-1));
	                        }
	                    }
	                    //clu=0;
	                }
	
	                if(i == currentRow && j == currentColumn)
	                {
	                    g.setColor(Color.RED);
	                    g.fillRect(x, y, GRID_WIDTH, GRID_HEIGHT);
	                }  
	                g.setColor(Color.BLUE);
	                //g.setFont(Utility.getInstance().getGujaratiFont("rekha", 5));
	                if(values[i][j]!=".")
	                    g.drawText("" + values[i][j], x + 5, y + 11);
	                g.setColor(Color.BLACK);
	                g.setFont(numericFont);
	                if(numbers[i][j]!=0)
	                    g.drawText("" + numbers[i][j],x + 2  ,y + 2);
	                if(values[i][j] == "0"){
	                    g.setColor(Color.BLACK);
	                    g.fillRect(x, y, GRID_WIDTH, GRID_HEIGHT);
	                    //Field.VISUAL_STATE_FOCUS;
	                }
	                x = x + GRID_WIDTH - 1;      
	            }
	            x = 0;
	            y = y + GRID_HEIGHT - 1;  
	
	        }
	        //clu=0;
	    } 
	    catch (Exception e) 
	    {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    }
	}
	
	
	protected  int  moveFocus(int amount, int status, int time) 
	{
	    System.out.println("moveFocus -=======>");
	    return amount;
	}
	
	protected  boolean navigationMovement(int dx, int dy, int status, int time) 
	{
	    clu = 0;
	    System.out.println("navigationMovement -=======dx>"+dx);
	    System.out.println("navigationMovement -=======dy>"+dy);
	    if(dx == 1)
	    {
	        if(currentColumn < numRows - 1)
	        {
	            currentColumn ++;
	        }
	    }
	    else if(dx == -1)
	    {
	        if(currentColumn > 0)
	        {
	            currentColumn --;
	        }            
	    }
	    else if(dy == 1)
	    {
	        if(currentRow < numRows - 1)
	        {
	            currentRow ++;
	        }
	       // else
	            //TabControlScreen.gridfocus();
	    }
	    else if(dy == -1)
	    {
	        if(currentRow > 0)
	        {
	            currentRow --;
	        }
	    }
	    invalidate();
	    return true;
	}
	
	public void drawText(String string) 
	{
	
	    try 
	    {
	        System.out.println("inside keyChar ====>" + string);
	        System.out.println("CurrentRow:::" + currentRow + "CurrentColumn:::"
	                + currentColumn + "Values:::"
	                + values[currentRow][currentColumn]);
	        if (!values[currentRow][currentColumn].equalsIgnoreCase("0")) 
	        {
	            values[currentRow][currentColumn] = string;// String.valueOf(string);
	            System.out.println("GridField::Values::string::"+string);
	            invalidate();
	        } 
	        else
	            Dialog.alert("Invalid Field!");
	    } 
	    catch (Exception e) 
	    {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    }
	    //return true;
	}
	
	
	public String getText() 
	{
	    return "";
	
	}
	
	public String returnText() 
	{
	    return values[currentRow][currentColumn];
	
	}
	public void pintargrid()
	{
		for (int row =  0 ; row < lista_articulo.cantElementListaArticulo() ; row++)
		{
			values[row][0] = lista_articulo.getArticuloCarritoAtIndex(row).
					getArticulo().getNombreArticulo();
			values[row][1] = ""+lista_articulo.getArticuloCarritoAtIndex(row).
					getCantidadSeleccionada();
			values[row][2] = ""+(lista_articulo.getArticuloCarritoAtIndex(row).
					getArticulo().getPrecioUnitario()*lista_articulo.getArticuloCarritoAtIndex(row).
					getCantidadSeleccionada());
			
		}
	}


}
